package com.hsd.whb.utils;

import java.util.*;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.util.StringUtils;


public class BeanUtil {

    /**
     * 获取对象中空值属性
     *
     * @param source
     * @return
     */
    public static String[] getNullPropertyNames(Object source) {
        final BeanWrapper src = new BeanWrapperImpl(source);
        java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

        Set<String> emptyNames = new HashSet<String>();
        for (java.beans.PropertyDescriptor pd : pds) {
            Object srcValue = src.getPropertyValue(pd.getName());
            if (srcValue == null) emptyNames.add(pd.getName());
        }
        String[] result = new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }

    /**
     * 获取对象中空值属性
     *
     * @param source
     * @return
     */
    public static String[] getEmptyPropertyNames(Object source) {
        final BeanWrapper src = new BeanWrapperImpl(source);
        java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

        Set<String> emptyNames = new HashSet<String>();
        for (java.beans.PropertyDescriptor pd : pds) {
            if (pd.getName().equals("class")) continue;
            Object srcValue = src.getPropertyValue(pd.getName());
            if (srcValue == null) emptyNames.add(pd.getName());
            if (srcValue instanceof String && StringUtils.isEmpty(srcValue)) {
                emptyNames.add(pd.getName());
            }
        }
        String[] result = new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }

    /**
     * 获取对象中非空值属性
     *
     * @param source
     * @return
     */
    public static String[] getNotEmptyPropertyNames(Object source) {
        final BeanWrapper src = new BeanWrapperImpl(source);
        java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

        Set<String> emptyNames = new HashSet<String>();
        for (java.beans.PropertyDescriptor pd : pds) {
            if (pd.getName().equals("class")) continue;
            Object srcValue = src.getPropertyValue(pd.getName());
            if (srcValue instanceof String && !StringUtils.isEmpty(srcValue)) {
                emptyNames.add(pd.getName());
            }
            if (srcValue != null) emptyNames.add(pd.getName());
        }
        String[] result = new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }

    /**
     * 获取对象中属性值
     *
     * @param source
     * @return
     */
    public static Object getValueByPropertyNames(Object source, String propertyNames) {
        BeanWrapper src = new BeanWrapperImpl(source);
        return src.getPropertyValue(propertyNames);
    }

    /**
     * 设置对象中属性值
     *
     * @param source
     * @return
     */
    public static void setValueByPropertyNames(Object source, String propertyNames, Object value) {
        BeanWrapper src = new BeanWrapperImpl(source);
        src.setPropertyValue(propertyNames, value);
    }

    /**
     * 忽略空值
     *
     * @param source
     * @param target
     * @return
     */
    public static void copyPropertiesIgnoreNull(Object source, Object target) {
        BeanUtils.copyProperties(source, target, getNullPropertyNames(source));
    }

    /**
     * 忽略空值
     *
     * @param source
     * @param target
     * @return
     */
    public static void copyPropertiesIgnoreEmpty(Object source, Object target) {
        BeanUtils.copyProperties(source, target, getEmptyPropertyNames(source));
    }

    /**
     * 不忽略空值
     *
     * @param source
     * @param target
     */
    public static void copyProperties(Object source, Object target) {
        BeanUtils.copyProperties(source, target);
    }


}
